package com.yingpai.fitness.dvp_2_0.main.fragment.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.main.activity.dynamic.PublishActivity;
import com.yingpai.fitness.dvp_2_0.common.fragment.CommonFragment;
import com.yingpai.fitness.dvp_2_0.common.view.statusbar.StatusView;
import com.yingpai.fitness.dvp_2_0.main.adapter.DynamicPageAdapter;
import com.yingpai.fitness.dvp_2_0.main.fragment.dynamic.AttentionDynamicFragment;
import com.yingpai.fitness.dvp_2_0.main.fragment.dynamic.HotDynamicFragment;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.entity.TabEntity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 动态fragment
 * Created by ASUS on 2018/11/2.
 */

public class DynamicFragment extends CommonFragment {
    @BindView(R.id.status_view)
    StatusView statusView;
    Unbinder unbinder;
    @BindView(R.id.tablayout)
    CommonTabLayout tablayout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    private DynamicPageAdapter adapter;
    /**
     * Fragment集合
     */
    private ArrayList<Fragment> fList = new ArrayList<>();
    /**
     * 默认标题栏数组
     */
    private String[] mTitleParentTopTab = {"热门", "关注", "同城"};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    public static DynamicFragment newInstance() {
        return new DynamicFragment();
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dvp_2_0_fragment_dynamic, container, false);
        unbinder = ButterKnife.bind(this, view);
        viewpager.setOffscreenPageLimit(mTitleParentTopTab.length);

        fList.add(HotDynamicFragment.getInstance(Constant.NUM_1));//热门的网络请求参数是1
        fList.add(AttentionDynamicFragment.getInstance());
        fList.add(HotDynamicFragment.getInstance(Constant.NUM_3));//同城的网络请求参数是3
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        statusView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.normal_status));
        /**
         * getFragmentManager()所得到的是所在fragment 的父容器的管理器，
         * getChildFragmentManager()所得到的是在fragment  里面子容器的管理器。
         */
        adapter = new DynamicPageAdapter(this.getChildFragmentManager(), fList, mTitleParentTopTab);
        viewpager.setAdapter(adapter);

        for (int i = 0; i < mTitleParentTopTab.length; i++) {
            mTabEntities.add(new TabEntity(mTitleParentTopTab[i], 0, 0));
        }
        tablayout.setTabData(mTabEntities);
        tablayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewpager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tablayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }

        });
        viewpager.setCurrentItem(0);
    }

    @Override
    protected void initData() {

    }

    @Override
    @OnClick({R.id.dynamic_publish_iv})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.dynamic_publish_iv:
                Bundle bundle = new Bundle();
                bundle.putString("flag","dynamic");
                startActivity(PublishActivity.class,bundle);
                break;
        }
    }

}
